package com.vchello.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.vchello.bean.security.Role;
import com.vchello.bean.security.UserRole;
import com.vchello.bean.user.Invitation;
import com.vchello.bean.user.User;
import com.vchello.service.security.RoleService;
import com.vchello.service.security.UserRoleService;
import com.vchello.service.user.InvitationService;
import com.vchello.service.user.UserService;
import com.vchello.thead.RemoveInvitationThread;
import com.vchello.util.RandomUtil;
import com.vchello.util.SmsUtil;
import com.vchello.util.Utility;

/**
 * 邀请成员
 * @author power
 *
 */
@Controller
@RequestMapping(value = "/invitation")
public class InvitationController extends BaseController {
	
	private static final String REDIRECT_LOGIN = "redirect:/login";
	private static final int[] SEND_STATUS = {1, 2};
	
	@Autowired
	private RoleService roleService;
	@Autowired
	private InvitationService invitationService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private RemoveInvitationThread removeInvitation;
	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;

	@RequestMapping(value = "/send", method = RequestMethod.GET)
	public String invitation(Model model){
		User user = getUser();
		if(null == user) {
			return REDIRECT_LOGIN;
		}
		List<Role> roles = roleService.findRoleByUserId(user.getId());
		model.addAttribute("roleList", roles);
		return "/user/inviteRole";
		
	}
	
	@RequestMapping(value = "/send", method = RequestMethod.POST)
	public void invitation(Invitation invitation){
		try {
		
		User user = getUser();
		if(null == user) {
			error("用户未登录");
			return;
		}
		
		if(null == invitation){
			error("信息为空");
			return;
		}
		User invitationUser = userService.findUserByPhone(invitation.getPhone());
		if(null != invitationUser) {
			if(null != invitationUser.getUserRole()) {
				error("该用户已有加入机构,让解除再邀请");
				return;
			}
		}
		invitation.setInviteId(user.getId());
		invitation.setReceiveId(null == invitationUser ? 0 : invitationUser.getId());
		invitation.setCreateTime(Utility.getSimpleFormatedDayNow());
		invitation.setSendStatus(SEND_STATUS[0]);
		String code = RandomUtil.randomCode();
		invitation.setCode(code);
		boolean isTrue = invitationService.saveInvitation(invitation);
		if(isTrue) {
			//发邮件
			String content = "欢迎使用VChello SaaS股权投资管理工具，微投网现邀您加入，登录时输入邀请码：" + code + "激活";
			SmsUtil.batchSend(invitation.getPhone(), content, true, null, null);
		}
		success("邀请成员成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/success", method = RequestMethod.GET)
	public String success(){
		return "user/success";
		
	}
	
	@RequestMapping(value = "/{inviteId}/{receiveId}", method = RequestMethod.POST)
	public void remove(@PathVariable int inviteId, @PathVariable int receiveId){
		Invitation invitation = invitationService.findInvitation(inviteId, receiveId);
		if(null != invitation) {
			boolean isTrue = invitationService.deleteInvitation(invitation);
			if(isTrue) {
				UserRole userRole = userRoleService.findUserRole(receiveId);
				userRoleService.deleteUserRole(userRole);
				//备份
				removeInvitation.setInvestorId(receiveId);
				removeInvitation.setOrganizetionId(inviteId);
				threadPoolTaskExecutor.execute(removeInvitation);
				success("移除成功");
				return;
			}
		}
		success("移除失败");
	}
	
}
